package dao;

import baperjakat.Koneksi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.Pegawai;

/**
 *
 * @author Marlon
 */
public class PegawaiDao {
    private Connection c;
    private PreparedStatement p;
    private Statement s;
    
    public PegawaiDao(){
        c = Koneksi.getKoneksi();
    }
    public void insert(Pegawai m) throws SQLException {
        p = c.prepareStatement("INSERT INTO datapegawai( nama, tanggallahir, agama, tahunmasuk, golongan, jabatan, alamat, nip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        simpan(m, p);
    }
     
    public void update(Pegawai m) throws SQLException {
        p = c.prepareStatement("UPDATE datapegawai SET nama = ?, tanggallahir = ?, agama = ?, tahunmasuk = ?, golongan = ?, jabatan = ?, alamat = ? WHERE nip = ?");
        simpan(m, p);
    }
    
    public List<Pegawai> cari(String nama) throws SQLException {
        List<Pegawai> listPegawai = new ArrayList();
        p = c.prepareStatement("select * from datapegawai where nama LIKE '%"+nama+"%'");

        ResultSet r = p.executeQuery();
                
        Pegawai m = null;
        while(r.next()) {
            m = new Pegawai();
            m.setNip(r.getString("Nip"));
            m.setNama(r.getString("Nama"));
            m.setTanggalLahir(r.getDate("TanggalLahir"));
            m.setAgama(r.getString("Agama"));
            m.setTahunMasuk(r.getString("TahunMasuk"));
            m.setGolongan(r.getString("Golongan"));
            m.setJabatan(r.getString("jabatan"));
            m.setAlamat(r.getString("Alamat"));
            
            listPegawai.add(m);
        }

        return listPegawai;
    }

    private void simpan(Pegawai m, final PreparedStatement p) throws SQLException {
        p.setString(1, m.getNama());
        p.setDate(2, m.getTanggalLahir());
        p.setString(3, m.getAgama());
        p.setString(4, m.getTahunMasuk());
        p.setString(5, m.getGolongan());
        p.setString(6, m.getJabatan());
        p.setString(7, m.getAlamat());
        p.setString(8, m.getNip());

        p.executeUpdate();
        p.close();
    }
    
    public void delete(Pegawai m) throws SQLException {
        p = c.prepareStatement("DELETE FROM datapegawai WHERE nip = ?");
            
        p.setString(1, m.getNip());
        p.executeUpdate();
        p.close();
    }
    
    public List<Pegawai> getAll() throws SQLException {
        List<Pegawai> listPegawai = new ArrayList<>();
        
        s = c.createStatement();
        String sql = "SELECT * FROM datapegawai";
        ResultSet r = s.executeQuery(sql);
        
        Pegawai m = null;
        while(r.next()) {
            m = new Pegawai();
            m.setNip(r.getString("Nip"));
            m.setNama(r.getString("Nama"));
            m.setTanggalLahir(r.getDate("TanggalLahir"));
            m.setAgama(r.getString("Agama"));
            m.setTahunMasuk(r.getString("TahunMasuk"));
            m.setGolongan(r.getString("Golongan"));
            m.setJabatan(r.getString("jabatan"));
            m.setAlamat(r.getString("Alamat"));
            
            listPegawai.add(m);
        }
        
        r.close();
        s.close();

        return listPegawai;
    }
}
